package com.it.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it.entity.AnswerRecord;
import com.it.mapper.AnswerRecordMapper;
import com.it.service.IAnswerRecordService;
import com.it.util.ThreadLocalUtil;
import com.it.vo.AnswerRecordVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  答题记录实现类
 * </p>
 *
 * @author author
 * @since 2023-09-26
 */
@Service
public class AnswerRecordServiceImpl implements IAnswerRecordService {

    @Resource
    AnswerRecordMapper answerRecordMapper;
    @Override
    public void addAnswerRecord(AnswerRecord answerRecord) {
        answerRecordMapper.addAnswerRecord(answerRecord);
    }

    @Override
    public List<AnswerRecordVo> getAnswerRecordsByUserId(Long userId) {
        List<AnswerRecordVo> records = answerRecordMapper.getAnswerRecordsByUserId(userId);
//        处理数据格式(题目内容，你的答案，正确答案，提交时间)
        records.forEach(answerRecordVo -> {
            String options = answerRecordVo.getQuestionOptions();
            JSONObject jsonObject = JSON.parseObject(options);
            answerRecordVo.setUserAnswer(jsonObject.getString(answerRecordVo.getUserAnswer()));
            answerRecordVo.setTrueAnswer(jsonObject.getString(answerRecordVo.getTrueAnswer()));
            answerRecordVo.setQuestionOptions(null);
        });
        return records;
    }
}
